//
//  UTabBarController.swift
//  U17
//
//  Created by charles on 2017/9/29.
//  Copyright © 2017年 None. All rights reserved.
//

import UIKit

class UTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.isTranslucent = false
        
        let homeVC = HomeViewController()
        addChildViewController(homeVC,
                               title: "首页",
                               image: #imageLiteral(resourceName: "home-normal"),
                               selectedImage: #imageLiteral(resourceName: "home-active"))
        /// 订单
        let orderVC = OrderViewController()
        addChildViewController(orderVC,
                               title: "订单",
                               image: #imageLiteral(resourceName: "home-normal"),
                               selectedImage: #imageLiteral(resourceName: "home-active"))
        //消息
        let msgVC = MessageViewController()
        addChildViewController(msgVC,
                               title: "消息",
                               image: #imageLiteral(resourceName: "home-normal"),
                               selectedImage: #imageLiteral(resourceName: "home-active"))
        /// 我的
        let mineVC = UserViewController()
        addChildViewController(mineVC,
                               title: "我的",
                               image: #imageLiteral(resourceName: "home-normal"),
                               selectedImage: #imageLiteral(resourceName: "home-active"))
    }
    
    func addChildViewController(_ childController: UIViewController, title:String?, image:UIImage? ,selectedImage:UIImage?) {
        
        
        childController.tabBarItem = UITabBarItem(title: title,
                                                  image: image?.withRenderingMode(.alwaysOriginal),
                                                  selectedImage: selectedImage?.withRenderingMode(.alwaysOriginal))
        
        //        if UIDevice.current.userInterfaceIdiom == .phone {
        //            childController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        //        }
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.colHui, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)], for: .selected)
        // 修改标签栏未选中时文字颜色、字体
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.colHuiwhite, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)], for: .normal)
        
        addChild(UNavigationController(rootViewController: childController))
        //        addChild(childController)
    }
    
}

extension UTabBarController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        guard let select = selectedViewController else { return .lightContent }
        return select.preferredStatusBarStyle
    }
}
